package com.aw.baselib.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aw.baselib.R;
import com.aw.baselib.widgets.AwRecycleViewDivider;
import com.aw.baselib.widgets.AwRecyclerViewNoBugLinearLayoutManager;
import com.aw.baselib.widgets.AwViewCustomDivider;
import com.aw.baselib.widgets.SpaceItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


/**
 * Created by hzw
 */

public class AwRecyclerViewUtil {

    public static void resetView(Activity activity, BaseQuickAdapter adapter, RecyclerView recyclerView, String emptyTxt) {
        adapter.setNewData(null);
        recyclerView.removeAllViews();
        adapter.setHeaderAndEmpty(true);
        adapter.setEmptyView(AwRecyclerViewUtil.getEmptyDataView(activity, emptyTxt, -1));
    }

    public static void resetView(Activity activity, BaseQuickAdapter adapter, RecyclerView recyclerView) {
        adapter.setNewData(null);
        recyclerView.removeAllViews();
        adapter.setHeaderAndEmpty(true);
        adapter.setEmptyView(AwRecyclerViewUtil.getEmptyDataView(activity, activity.getResources().getString(R.string.common_no_data), -1));
    }

    public static void resetView(Activity activity, BaseQuickAdapter adapter, RecyclerView recyclerView, View.OnClickListener onClickListener) {
        adapter.setNewData(null);
        recyclerView.removeAllViews();
        adapter.setHeaderAndEmpty(true);
        View emptyView = AwRecyclerViewUtil.getEmptyDataView(activity, activity.getResources().getString(R.string.common_no_data), -1);
        if(onClickListener != null) {
            emptyView.setOnClickListener(onClickListener);
        }
        adapter.setEmptyView(emptyView);
    }

    public static void resetView(Activity activity, BaseQuickAdapter adapter, RecyclerView recyclerView, boolean resetView) {
        if(resetView) {
            adapter.setNewData(null);
            recyclerView.removeAllViews();
            adapter.setHeaderAndEmpty(true);
            adapter.setEmptyView(AwRecyclerViewUtil.getEmptyDataView(activity, activity.getResources().getString(R.string.common_no_data), -1));
        }
    }

    public static void resetView(Activity activity, BaseQuickAdapter adapter, RecyclerView recyclerView, boolean resetView, View.OnClickListener onClickListener) {
        if(resetView) {
            adapter.setNewData(null);
            recyclerView.removeAllViews();
            adapter.setHeaderAndEmpty(true);
            View emptyView = AwRecyclerViewUtil.getEmptyDataView(activity, activity.getResources().getString(R.string.common_no_data), -1);
            if(onClickListener != null) {
                emptyView.setOnClickListener(onClickListener);
            }
            adapter.setEmptyView(emptyView);
        }
    }

    /**
     * 通用 SwipeRefreshLayout 设置
     * @param srlLayout
     * @param isEnable
     */
    public static void setSwipeRefreshLayout(SwipeRefreshLayout srlLayout, boolean isEnable) {
        srlLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        srlLayout.setDistanceToTriggerSync(300);
        srlLayout.setSize(SwipeRefreshLayout.DEFAULT);
        srlLayout.setEnabled(isEnable);
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     */
    public static void setRecyclerViewLinearlayoutHorizontal(Activity activity, RecyclerView recyclerView, BaseQuickAdapter mAdapter) {
        AwRecyclerViewNoBugLinearLayoutManager layoutManager = new AwRecyclerViewNoBugLinearLayoutManager(activity);
        layoutManager.setOrientation(AwRecyclerViewNoBugLinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mAdapter);
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param showItemDecoration
     */
    public static void setRecyclerViewLinearlayout(Activity activity, RecyclerView recyclerView, BaseQuickAdapter mAdapter, boolean showItemDecoration) {
        AwRecyclerViewNoBugLinearLayoutManager layoutManager = new AwRecyclerViewNoBugLinearLayoutManager(activity);
        layoutManager.setOrientation(AwRecyclerViewNoBugLinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        if(showItemDecoration) {
            recyclerView.addItemDecoration(new AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    AwDisplayUtil.dip2px(activity, 0.5f), activity.getResources().getColor(R.color.color_ebebeb)));
        }
//        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(mAdapter);
    }

    public static void setRecyclerViewLinearlayoutNoAnimation(Activity activity, RecyclerView recyclerView, BaseQuickAdapter mAdapter, boolean showItemDecoration) {
        AwRecyclerViewNoBugLinearLayoutManager layoutManager = new AwRecyclerViewNoBugLinearLayoutManager(activity);
        layoutManager.setOrientation(AwRecyclerViewNoBugLinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        if(showItemDecoration) {
            recyclerView.addItemDecoration(new AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    AwDisplayUtil.dip2px(activity, 0.5f), activity.getResources().getColor(R.color.color_ebebeb)));
        }
        recyclerView.setAdapter(mAdapter);
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param showItemDecoration
     */
    public static void setRecyclerViewLinearlayoutJudgeAutoSetAdapter(Activity activity, RecyclerView recyclerView, BaseQuickAdapter mAdapter, boolean showItemDecoration, boolean autoSetAdapter) {
        AwRecyclerViewNoBugLinearLayoutManager layoutManager = new AwRecyclerViewNoBugLinearLayoutManager(activity);
        layoutManager.setOrientation(AwRecyclerViewNoBugLinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        if(showItemDecoration) {
            recyclerView.addItemDecoration(new AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    AwDisplayUtil.dip2px(activity, 0.5f), activity.getResources().getColor(R.color.color_ebebeb)));
        }
//        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        if(autoSetAdapter) {
            recyclerView.setAdapter(mAdapter);
        }
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param decorationHeightF
     * @param autoSetAdapter
     */
    public static void setRecyclerViewLinearlayoutWithDecorationHeight(Activity activity, RecyclerView recyclerView, BaseQuickAdapter mAdapter,
                                                                       float decorationHeightF, boolean autoSetAdapter) {
        AwRecyclerViewNoBugLinearLayoutManager layoutManager = new AwRecyclerViewNoBugLinearLayoutManager(activity);
        layoutManager.setOrientation(AwRecyclerViewNoBugLinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new AwRecycleViewDivider(
                activity, LinearLayoutManager.HORIZONTAL,
                AwDisplayUtil.dip2px(activity, decorationHeightF), activity.getResources().getColor(R.color.color_ebebeb)));
//        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
//        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        if(autoSetAdapter)
            recyclerView.setAdapter(mAdapter);
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param showItemDecoration
     */
    public static void setRecyclerViewLinearlayout(Activity activity, RecyclerView recyclerView, BaseQuickAdapter mAdapter, LinearLayoutManager layoutManager, boolean showItemDecoration, boolean autoSetAdapter) {
        //        RecyclerViewNoBugLinearLayoutManager layoutManager = new RecyclerViewNoBugLinearLayoutManager(activity);
        layoutManager.setOrientation(AwRecyclerViewNoBugLinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        if(showItemDecoration) {
            recyclerView.addItemDecoration(new AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    AwDisplayUtil.dip2px(activity, 0.5f), activity.getResources().getColor(R.color.color_ebebeb)));
        }
//        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        if(autoSetAdapter)
            recyclerView.setAdapter(mAdapter);
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param adapter
     */
    public static void setRecyclerViewGridlayout(Activity activity, RecyclerView recyclerView, BaseQuickAdapter adapter, int spanCount) {
        GridLayoutManager manager = new GridLayoutManager(activity, spanCount);
//        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        manager.supportsPredictiveItemAnimations();
        recyclerView.setLayoutManager(manager);
//        recyclerView.addItemDecoration((new AwViewCustomDivider(AwDisplayUtil.dip2px(activity, 5))));
//        DefaultItemAnimator animator = new DefaultItemAnimator();
//        recyclerView.setItemAnimator(animator);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param adapter
     */
    public static void setRecyclerViewGridlayout(Activity activity, RecyclerView recyclerView, BaseQuickAdapter adapter, int spanCount, int dividerValue) {
        GridLayoutManager manager = new GridLayoutManager(activity, spanCount);
//        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        manager.supportsPredictiveItemAnimations();
        recyclerView.setLayoutManager(manager);
        if(recyclerView.getTag(recyclerView.getId()) != null && recyclerView.getTag(recyclerView.getId()).equals("addedDecoration")) {

        } else {
            recyclerView.addItemDecoration((new SpaceItemDecoration(AwDisplayUtil.dip2px(activity, dividerValue), spanCount)));
            recyclerView.setTag(recyclerView.getId(), "addedDecoration");
        }
//        DefaultItemAnimator animator = new DefaultItemAnimator();
//        recyclerView.setItemAnimator(animator);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 根据item长度动态控制每行item数量
     * @param activity
     * @param manager
     * @param recyclerView
     * @param adapter
     * @param dividerValue
     */
    public static void setRecyclerViewGridlayout(Activity activity, GridLayoutManager manager, RecyclerView recyclerView, BaseQuickAdapter adapter, int dividerValue) {
//        manager.supportsPredictiveItemAnimations();
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration((new AwViewCustomDivider(AwDisplayUtil.dip2px(activity, dividerValue))));
//        DefaultItemAnimator animator = new DefaultItemAnimator();
//        recyclerView.setItemAnimator(animator);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 空数据页面展示
     * @param context
     * @param tag 0 未定时间 1 发起/参与  2 无信息 3 无数据
     * @return
     */
    public static View getEmptyDataView(Context context, int tag, int drawableId) {
        AwLog.d("getEmptyDataView tag: " + tag);
        View emptyView = LayoutInflater.from(context).inflate(R.layout.view_no_data, null);
        TextView tv_noData = (TextView) emptyView.findViewById(R.id.tv_noData);
        if(-1 != drawableId) {
            setDrawableDirection(context, tv_noData,3, drawableId);
        }
        switch (tag) {
            default:
                tv_noData.setText(context.getResources().getString(R.string.common_no_data));
                break;
        }
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        return emptyView;
    }

    /**
     * 空数据页面展示
     * @param context
     * @return
     */
    public static View getEmptyDataView(Context context, String noDataTxt, int drawableId) {
        if(AwDataUtil.isEmpty(noDataTxt)) {
            noDataTxt = context.getResources().getString(R.string.common_no_data);
        }
        View emptyView = LayoutInflater.from(context).inflate(R.layout.view_no_data, null);
        TextView tv_noData = (TextView) emptyView.findViewById(R.id.tv_noData);
        if(-1 != drawableId) {
            setDrawableDirection(context, tv_noData,3, drawableId);
        }
        tv_noData.setText(noDataTxt);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        return emptyView;
    }

    /**
     * 左1右2上3下4
     */
    public static void setDrawableDirection(Context context, TextView tv, int tag, int resId) {
        Drawable drawable = context.getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        switch (tag) {
            case 1:
                tv.setCompoundDrawables(drawable, null, null, null);
                break;
            case 2:
                tv.setCompoundDrawables(null, null, drawable, null);
                break;
            case 3:
                tv.setCompoundDrawables(null, drawable, null, null);
                break;
            case 4:
                tv.setCompoundDrawables(null, null, null, drawable);
                break;
        }
    }

    public static void cancelDrawableDirection(Context context, TextView tv) {
        tv.setCompoundDrawables(null, null, null, null);
    }
}
